package com.green.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.green.business.domain.Goods;
import com.green.business.domain.Provider;
import com.green.business.service.GoodsService;
import com.green.business.service.ProviderService;
import com.green.business.service.ScrapService;
import com.green.business.vo.ScrapVo;
import com.green.system.common.Constant;
import com.green.system.common.DataGridView;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.business.mapper.ScrapMapper;
import com.green.business.domain.Scrap;

import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2021/12/03/22:01
 * @Description: 
 */


@Service
public class ScrapServiceImpl extends ServiceImpl<ScrapMapper, Scrap> implements ScrapService {

    @Autowired
    private ScrapMapper scrapMapper;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProviderService providerService;

    @Override
    public DataGridView queryAllScrap(ScrapVo scrapVo) {
        IPage<Scrap> page = new Page<>(scrapVo.getPage(),scrapVo.getLimit());
        QueryWrapper<Scrap> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(scrapVo.getGoodsid()!=null,"goodsid",scrapVo.getGoodsid());
        queryWrapper.eq(scrapVo.getProviderid()!=null,"providerid",scrapVo.getProviderid());

        queryWrapper.eq(scrapVo.getStartTime()!=null,"scraptime",scrapVo.getStartTime());
        queryWrapper.eq(scrapVo.getEndTime()!=null,"scraptime",scrapVo.getEndTime());
        queryWrapper.orderByDesc("scraptime");

        this.scrapMapper.selectPage(page,queryWrapper);
        List<Scrap> records = page.getRecords();
        for (Scrap record : records) {
            if (null != record.getGoodsid()){
                Goods goods = this.goodsService.getById(record.getGoodsid());
                record.setGoodsname(goods.getGoodsname());
                record.setSize(goods.getSize());
            }
            if (null != record.getProviderid()){
                Provider provider = this.providerService.getById(record.getProviderid());
                record.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    @Override
    public Scrap saveScrap(Scrap scrap) {
        //新建报废商品明细
        this.scrapMapper.insert(scrap);

        //修改未上架商品数
        Goods goods = this.goodsService.getById(scrap.getGoodsid());
        goods.setNumber(goods.getNumber()-scrap.getNumber());
        goods.setAvailable(Constant.AVAILABLE_FALSE);
        this.goodsService.updateGoods(goods);
        return scrap;
    }

    @Override
    public Integer querySalesNumberById(Integer goodsid) {
        Integer a = this.scrapMapper.querySalesNumberById(goodsid);
        if (a == null || a.equals("")){
            return 0 ;
        }
        return a;
    }
}
